package com.pinyougou.sellergoods.service.impl;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import com.pinyougou.pojo.TbBrandExample;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;

import static com.pinyougou.pojo.TbBrandExample.*;

@Service
public class BrandServiceImpl implements BrandService {

	@Autowired
	private TbBrandMapper brandMapper;
	
	@Override
	public List<TbBrand> findAll() {

		return brandMapper.selectByExample(null);
	}

	/**
	 * 分页查询品牌
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageResult findPage(Integer pageNum, Integer pageSize) {

		PageHelper.startPage(pageNum,pageSize);
		Page<TbBrand> tbBrands = (Page<TbBrand>) brandMapper.selectByExample(null);
		Long total = tbBrands.getTotal();
		List<TbBrand> result = tbBrands.getResult();
		PageResult pageResult = new PageResult(total,result);
		return pageResult;
	}

	@Override
	public PageResult findPage(TbBrand tbBrand, Integer pageNum, Integer pageSize) {

		PageHelper.startPage(pageNum,pageSize);
		TbBrandExample example = new TbBrandExample();
		Criteria criteria = example.createCriteria();

		if (tbBrand!= null) {
			if (tbBrand.getName() != null && tbBrand.getName().length()>0) {
				criteria.andNameLike("%"+tbBrand.getName()+"%");
			}
			if (tbBrand.getFirstChar() != null && tbBrand.getFirstChar().length()>0) {
				criteria.andFirstCharLike("%"+tbBrand.getFirstChar()+"%");
			}
		}

		Page<TbBrand> tbBrands = (Page<TbBrand>) brandMapper.selectByExample(example);
		Long total = tbBrands.getTotal();
		List<TbBrand> result = tbBrands.getResult();
		PageResult pageResult = new PageResult(total,result);
		return pageResult;
	}

	/**
	 * 添加品牌
	 * @param tbBrand
	 */
	@Override
	public void saveBrand(TbBrand tbBrand) {
		brandMapper.insert(tbBrand);
	}

	/**
	 * 通过id查找品牌对象
	 * @param id
	 * @return
	 */
	@Override
	public TbBrand findOne(Long id) {
		return brandMapper.selectByPrimaryKey(id);
	}

	@Override
	public void delete(Long[] ids) {
		for (Long id : ids) {
			brandMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public void update(TbBrand tbBrand) {

		brandMapper.updateByPrimaryKey(tbBrand);
	}

	@Override
	public List<Map> selectOptionList() {
		return brandMapper.selectOptionList();
	}
}
